var redis = require('redis');
var client = redis.createClient(6379, '127.0.0.1');
// var client = redis.createClient(6379, '59.110.173.122');

// client.auth(123456);  // 如果没有设置密码 是不需要这一步的


module.exports = {
    setData(key = "", data = {}) {
        //    console.log(data)
        let str = this.isJSON(data);
        let res = new Promise((success, fail) => {
            str = str.replace(/\"/g, "\'")
            // console.log(str);
            client.set(key, str);

            // console.log('数据存储成功');
            success({
                code: 200,
                data: {},
                msg: "写入成功"
            })
        })

        return res
    },
    getData(key) {
        let res = new Promise((success, fail) => {
            client.get(key, (err, data) => {
                let json = {}
                if (!err) {
                    try {
                        let jsonStr = data.replace(/\'/g, "\"")
                        json = this.isJSON(jsonStr)

                    } catch (e) {
                        json = {}
                    }

                    success({
                        code: 200,
                        data: json,
                        msg: "获取数据成功"
                    })
                } else {
                    fail(err)
                }
            })
        })
        return res
    },
    isJSON(str) {
        if (typeof str == 'string') {
            try {
                let obj = JSON.parse(str);
                return obj
            } catch (e) {
                console.log(e);
                return str;
            }
        }
        return JSON.stringify(str);
    },
    set(key, value) {
        client.set(key, value);

    },
    get() {
        return new Promise((success, fail) => {
            client.get('key01', function (err, val) {
                if (err) {
                    fail(err)
                } else {
                    success(val)
                }
            });
        })
    },
    append(key, value) {
        client.append(key, value);
    },
    add(key, number = 1) {
        client.incrby(key, number);
    },
    jian(key, number = 1) {
        client.decrby(key, number);
    },
    lPush(key, value) {
        client.lpush(key, value);
    },
    rPush(key, value) {
        client.rpush(key, value);
    },
    getList(key, start = 0, end = -1) {
        return new Promise((success, fail) => {
            client.lrange(key, start, end, function (err, val) {
                if (err) {
                    fail(err)
                } else {
                    success(val)
                }
            });
        })
    },
    hSet(name, key, value) {
        let data = {
            code: 200,
            data: {
                name, key, value
            },
            msg: "添加成功！"
        }
        console.log(data);
        client.hset(name, key, value);
        return data
    },
    hGet(name) {
        return new Promise((success, fail) => {
            client.hgetall(name, function (err, val) {
                if (err) {
                    fail(err)
                } else {
                    success(val)
                }
            });
        })
    },
    hGetValue(name, key) {
        console.log(name)
        console.log(key);
        return new Promise((success, fail) => {
            client.hget(name, key, function (err, val) {
                if (err) {
                    fail(err)
                } else {
                    success(val)
                }
            });
        })
    },
    isKey(name, key) {
        return new Promise((success, fail) => {
            client.hexists(name, key, function (err, val) {
                if (err) {
                    fail(err)
                } else {
                    success(val)
                }
            });
        })
    },
    Del(key) {
        let state = client.del(key);
        if (state) {
            return { code: 200, data: {}, msg: "删除成功!" }
        } else {
            return { code: 400, data: {}, msg: "删除失败!" }
        }
    },
    async hDel(name, key) {
        if (!key) {
            return { code: 400, data: {}, msg: "key不存在!" }
        }
        let state = client.hdel(name, key);
        if (state) {
            return { code: 200, data: {}, msg: "删除成功!" }
        } else {
            return { code: 400, data: {}, msg: "删除失败!" }
        }
    }
    // $redis ->;

}